import java.util.ArrayList;
import java.util.List;

class NewState1 extends PlanetWars implements Cloneable{

	private NewState1 parent;
	private String gameState;
	
	public static final int NEUTRAL = 0;
	public static final int FRIENDLY = 1;
	

	Planet preActionSource = null;
	Planet preActionDest = null;
	Planet parallelPreActionSource = null;
	Planet parallelPreActionDest = null;

	public NewState1(String gameStateString) {
		super(gameStateString);
		setGameState(gameStateString);
	}
	public NewState1(String gameStateString, NewState1 parent){
		super(gameStateString);
		setGameState(gameStateString);
		setParent(parent);
	}

	public int getScore(int i){
		int j;
		switch (i) {
		case 1:
			j =  Heuristics1.scoring1(this);
			break;
		case 2:
			j = Heuristics1.scoringUsingStateDepth(this);
			break;
		default:
			j = Heuristics1.scoring1(this);
			break;
		}
		return j;
	}

	@Override
	public NewState1 clone(){
		return new NewState1(this.toString());
	}


	public String getGameState() {
		return gameState;
	}
	public void setGameState(String gameState) {
		this.gameState = gameState;
	}
	public NewState1 getParent() {
		return parent;
	}
	public void setParent(NewState1 parent) {
		this.parent = parent;
	}

	public void SendFleet(Planet source, Planet dest) {
		// TODO Auto-generated method stub
		preActionSource = source;
		preActionDest = dest;

		int fleetSent = ( source.NumShips() - source.GrowthRate() ) / 2;
		Planet newActionDest = null;
		//boolean neutral = false;

		
		GetPlanet(source.PlanetID()).RemoveShips(fleetSent);
		newActionDest = GetPlanet(dest.PlanetID());
		int shipsLeftover = newActionDest.NumShips() - fleetSent;

		if (shipsLeftover >= 0)
		{
			newActionDest.RemoveShips(fleetSent);
		}
		else
		{
			newActionDest.RemoveShips(newActionDest.NumShips());
			newActionDest.Owner(source.Owner());
			newActionDest.AddShips(-shipsLeftover);
		}
	}
	
	public void SendFleetParallel(Planet source1, Planet dest1, Planet source2, Planet dest2)
	{
		preActionSource = source1;
		preActionDest = dest1;
		parallelPreActionSource = source2;
		parallelPreActionDest = dest2;
		
		int fleetSent1 = ( source1.NumShips()) / 2;		
		int fleetSent2 = ( source2.NumShips()) / 2;
		Planet newActionDest1 = null, newActionDest2 = null;
		
		//System.err.println("Source1: " + String.valueOf(source1 != null) + ", Source2: " + String.valueOf(source2 != null) + ", Dest1: " + String.valueOf(dest1 != null) + ", Dest2: " + String.valueOf(dest2 != null));
		GetPlanet(source1.PlanetID()).RemoveShips(fleetSent1);
		newActionDest1 = GetPlanet(dest1.PlanetID());
		GetPlanet(source2.PlanetID()).RemoveShips(fleetSent2);
		newActionDest2 = GetPlanet(dest2.PlanetID());
		int shipsLeftover1 = newActionDest1.NumShips() - fleetSent1;
		int shipsLeftover2 = newActionDest2.NumShips() - fleetSent2;

		if (shipsLeftover1 >= 0)
		{
			newActionDest1.RemoveShips(fleetSent1);
		}
		else
		{
			newActionDest1.RemoveShips(newActionDest1.NumShips());
			newActionDest1.Owner(source1.Owner());
			newActionDest1.AddShips(-shipsLeftover1);			
		}
		
		if (shipsLeftover2 >= 0)
		{
			newActionDest2.RemoveShips(fleetSent2);
		}
		else
		{
			newActionDest2.RemoveShips(newActionDest2.NumShips());
			newActionDest2.Owner(source2.Owner());
			newActionDest2.AddShips(-shipsLeftover2);
		}
		
		for (Planet p : this.Planets())
		{
			if (p.Owner()!= NEUTRAL)
				p.AddShips(p.GrowthRate());
		}
	}
	
	public List<NewState1> getChildren() {
		List<NewState1> children = new ArrayList<NewState1>();
		
		List<Planet> own = new ArrayList<Planet>();
		List<Planet> other = new ArrayList<Planet>();
		
		for(Planet p : Planets()){
			switch (p.Owner()) {
			case FRIENDLY:
				own.add(p);
				break;
			default:
				other.add(p);
				break;
			}
		}
		
		for(Planet ownP : own){
			for(Planet otherP : other){
				NewState1 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
		}
		
		return children;
	}
	
	// Voor minimax
	public List<NewState1> getChildren(boolean isMyTurn) {
		List<NewState1> children = new ArrayList<NewState1>();
		
		List<Planet> own = new ArrayList<Planet>();
		List<Planet> neutral = new ArrayList<Planet>();
		List<Planet> enemy = new ArrayList<Planet>();
		for(Planet p : Planets()){
			switch (p.Owner()) {
			case FRIENDLY:
				own.add(p);
				break;
			case NEUTRAL:
				neutral.add(p);
				break;
			default:
				enemy.add(p);
				break;
			}
		}
		if (!isMyTurn)
		{
			List<Planet> temp = own;
			own = enemy;
			enemy = temp;
		}

		for(Planet ownP : own){
			for(Planet otherP : enemy){
				NewState1 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
			for(Planet otherP : neutral){
				NewState1 tmp = this.clone();
				tmp.setParent(this);
				tmp.SendFleet(ownP, otherP);
				children.add(tmp);
			}
		}
		
		return children;
	}


}